<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15" style="margin-left: 30px;">
				<span class="p_el_input">名称：<el-input size="default" v-model="state.pagenation.name" placeholder="请输入考勤名称"
						clearable />
				</span>
				<el-button size="default" type="primary" text class="ml10" @click="getTableData">
					<el-icon style="font-size: 20px !important;">
						<ele-Search />
					</el-icon>
					{{ $t('message.btnName.search') }}
				</el-button>
				<el-button size="default" type="success" style="margin-left: 50px !important;" @click="onOpenAddDialog('添加考勤规则')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					{{ $t('message.btnName.addRow') }}
				</el-button>
			</div>
			<el-table border :data="state.list" v-loading="state.loading" style="width: 100%">
				<el-table-column prop="id" label="序号" width="70px" align="center">
					<template #default="{ $index }">
						{{ $index + 1 }}
					</template>
				</el-table-column>
				<el-table-column prop="name" label="名称" width="120" show-overflow-tooltip align="center">
					<template #default="scope">
						<div class="ellipsis">
							<el-link type="primary" @click="showRuleDetails(scope.row)">{{ scope.row.name }}</el-link>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="配置明细" width="120" show-overflow-tooltip align="center">
					<el-table-column label="工作日" width="400" show-overflow-tooltip align="center">
						<template #default="scope">
							<div v-if="scope.row.periodType === 2" class="ellipsis">
								<el-tag v-for="item in scope.row.periodList" style="margin-right: 10px;">
									{{ `周${week[item]}` }}</el-tag>
							</div>
							<div v-else class="ellipsis">
								<el-tag v-for="item in scope.row.periodList" style="margin-right: 10px;">
									{{ `${item}号` }}</el-tag>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="上班" show-overflow-tooltip align="center">
						<template #default="scope">
							{{ `${scope.row.signInTime}` }}
						</template>
					</el-table-column>
					<el-table-column label="下班" show-overflow-tooltip align="center">
						<template #default="scope">
							{{ `${scope.row.signOutTime}` }}
						</template>
					</el-table-column>
				</el-table-column>
				<el-table-column label="操作" fixed="right" width="100" align="center">
					<template #default="scope">
						<el-button :disabled="scope.row.notEdit === 1" size="small" text type="primary"
							@click="onOpenEditDialog('edit', scope.row.code)">	{{ $t('message.btnName.edit') }}</el-button>
						<el-button :disabled="scope.row.notDel === 1" size="small" text type="danger"
							@click="onRowDel(scope.row.code)">	{{ $t('message.btnName.remove') }}</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:pager-count="5" :page-sizes="[10, 50, 200, 800]" v-model:current-page="state.pagenation.pageNum" background
				v-model:page-size="state.pagenation.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="state.pagenation.total">
			</el-pagination>
		</el-card>
		<dialogComponent ref="dialogRef" @refresh="getTableData()" />
		<drawComponent ref="drawRef" />
	</div>
</template>

<script setup lang="ts" name="index">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useAttendRuleDeleteApi, useAttendRulePageApi } from '/@/api/attend/rule';

// 引入组件
const dialogComponent = defineAsyncComponent(() => import('./dialog.vue'));

import drawComponent from './editRule/ruleDraw.vue'
// 定义变量内容
const dialogRef = ref();
const drawRef = ref();
const urlStr = '/attend/rule'
const state = reactive({
	list: [] as any,
	loading: false,
	pagenation: {
		total: 0,
		pageNum: 1,
		pageSize: 10,
		name: '',
		devName: '',
		host: '',
	}
});

// 初始化表格数据
const getTableData = () => {
	state.loading = true;
	useAttendRulePageApi(state.pagenation).then(({ content }) => {
		state.list = content.list
		console.log(state.list)
		state.pagenation.total = content.total
		state.pagenation.pageNum = content.pageNum
		state.pagenation.pageSize = content.pageSize
	}).then(() => {
		state.loading = false;
	}).catch(() => {
		ElMessage.error('查询失败')
	})
};

const week: { [key: number]: string } = {
	0: '日',
	1: '一',
	2: '二',
	3: '三',
	4: '四',
	5: '五',
	6: '六',
};


const showRuleDetails = (row: object) => {
	drawRef.value.openDraw(row)
}

// 打开新增弹窗
const onOpenAddDialog = (option: string) => {
	dialogRef.value.openDialog(option, urlStr);
};
// 打开修改弹窗
const onOpenEditDialog = (option: string, code: string) => {
	dialogRef.value.openDialog(option, code);
};
// 删除
const onRowDel = (code: string) => {
	ElMessageBox.confirm(`此操作将永久删除，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			useAttendRuleDeleteApi(code).then(() => {
				ElMessage.success('删除成功');
			}).then(() => {
				getTableData();
			})
		})
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.pagenation.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.pagenation.pageNum = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;

		.el-table {
			flex: 1;
		}
	}
}

.is-underline {
	border-bottom: 1px solid #409eff;
}

.ellipsis {
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}
</style>
